// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


suite("test_cast_to_decimal256_from_double") {

    // This test case is generated from the correspoinding be UT test case,
    // update this case if the correspoinding be UT test case is updated,
    // e.g.: ../run-be-ut.sh --run --filter=FunctionCastToDecimalTest.* --gen_regression_case
    sql "set enable_decimal256 = true;"
    sql "drop table if exists test_cast_to_decimal_76_0_from_double_0_nullable;"
    sql "create table test_cast_to_decimal_76_0_from_double_0_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_0_from_double_0_nullable values (0, "0.03"),(1, "1.03"),(2, "9.03"),(3, "0.09"),(4, "1.09"),(5, "9.09"),(6, "-0.03"),(7, "-1.03"),(8, "-9.03"),(9, "-0.09"),(10, "-1.09"),(11, "-9.09")
      ,(12, null);
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as decimalv3(76, 0)) from test_cast_to_decimal_76_0_from_double_0_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as decimalv3(76, 0)) from test_cast_to_decimal_76_0_from_double_0_nullable order by 1;'

    sql "drop table if exists test_cast_to_decimal_76_0_from_double_0_not_nullable;"
    sql "create table test_cast_to_decimal_76_0_from_double_0_not_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_0_from_double_0_not_nullable values (0, "0.03"),(1, "1.03"),(2, "9.03"),(3, "0.09"),(4, "1.09"),(5, "9.09"),(6, "-0.03"),(7, "-1.03"),(8, "-9.03"),(9, "-0.09"),(10, "-1.09"),(11, "-9.09");
    """

    sql "set enable_strict_cast=true;"
    qt_sql_0_strict 'select f1, cast(f2 as decimalv3(76, 0)) from test_cast_to_decimal_76_0_from_double_0_not_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_0_non_strict 'select f1, cast(f2 as decimalv3(76, 0)) from test_cast_to_decimal_76_0_from_double_0_not_nullable order by 1;'

    sql "drop table if exists test_cast_to_decimal_76_38_from_double_1_nullable;"
    sql "create table test_cast_to_decimal_76_38_from_double_1_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_38_from_double_1_nullable values (0, "0.000000000000000000000000000000000000003"),(1, "0.000000000000000000000000000000000000013"),(2, "0.000000000000000000000000000000000000093"),(3, "0.099999999999999999999999999999999999993"),(4, "0.900000000000000000000000000000000000003"),(5, "0.900000000000000000000000000000000000013"),(6, "0.999999999999999999999999999999999999983"),(7, "0.999999999999999999999999999999999999993"),(8, "1.000000000000000000000000000000000000003"),(9, "1.000000000000000000000000000000000000013"),(10, "1.000000000000000000000000000000000000093"),(11, "1.099999999999999999999999999999999999993"),(12, "0.000000000000000000000000000000000000009"),(13, "0.000000000000000000000000000000000000019"),(14, "0.000000000000000000000000000000000000099"),(15, "0.099999999999999999999999999999999999999"),(16, "0.900000000000000000000000000000000000009"),(17, "0.900000000000000000000000000000000000019"),(18, "0.999999999999999999999999999999999999989"),(19, "0.999999999999999999999999999999999999999"),
      (20, "1.000000000000000000000000000000000000009"),(21, "1.000000000000000000000000000000000000019"),(22, "1.000000000000000000000000000000000000099"),(23, "1.099999999999999999999999999999999999999"),(24, "-0.000000000000000000000000000000000000003"),(25, "-0.000000000000000000000000000000000000013"),(26, "-0.000000000000000000000000000000000000093"),(27, "-0.099999999999999999999999999999999999993"),(28, "-0.900000000000000000000000000000000000003"),(29, "-0.900000000000000000000000000000000000013"),(30, "-0.999999999999999999999999999999999999983"),(31, "-0.999999999999999999999999999999999999993"),(32, "-1.000000000000000000000000000000000000003"),(33, "-1.000000000000000000000000000000000000013"),(34, "-1.000000000000000000000000000000000000093"),(35, "-1.099999999999999999999999999999999999993"),(36, "-0.000000000000000000000000000000000000009"),(37, "-0.000000000000000000000000000000000000019"),(38, "-0.000000000000000000000000000000000000099"),(39, "-0.099999999999999999999999999999999999999"),
      (40, "-0.900000000000000000000000000000000000009"),(41, "-0.900000000000000000000000000000000000019"),(42, "-0.999999999999999999999999999999999999989"),(43, "-0.999999999999999999999999999999999999999"),(44, "-1.000000000000000000000000000000000000009"),(45, "-1.000000000000000000000000000000000000019"),(46, "-1.000000000000000000000000000000000000099"),(47, "-1.099999999999999999999999999999999999999")
      ,(48, null);
    """

    sql "set enable_strict_cast=true;"
    qt_sql_1_strict 'select f1, cast(f2 as decimalv3(76, 38)) from test_cast_to_decimal_76_38_from_double_1_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_1_non_strict 'select f1, cast(f2 as decimalv3(76, 38)) from test_cast_to_decimal_76_38_from_double_1_nullable order by 1;'

    sql "drop table if exists test_cast_to_decimal_76_38_from_double_1_not_nullable;"
    sql "create table test_cast_to_decimal_76_38_from_double_1_not_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_38_from_double_1_not_nullable values (0, "0.000000000000000000000000000000000000003"),(1, "0.000000000000000000000000000000000000013"),(2, "0.000000000000000000000000000000000000093"),(3, "0.099999999999999999999999999999999999993"),(4, "0.900000000000000000000000000000000000003"),(5, "0.900000000000000000000000000000000000013"),(6, "0.999999999999999999999999999999999999983"),(7, "0.999999999999999999999999999999999999993"),(8, "1.000000000000000000000000000000000000003"),(9, "1.000000000000000000000000000000000000013"),(10, "1.000000000000000000000000000000000000093"),(11, "1.099999999999999999999999999999999999993"),(12, "0.000000000000000000000000000000000000009"),(13, "0.000000000000000000000000000000000000019"),(14, "0.000000000000000000000000000000000000099"),(15, "0.099999999999999999999999999999999999999"),(16, "0.900000000000000000000000000000000000009"),(17, "0.900000000000000000000000000000000000019"),(18, "0.999999999999999999999999999999999999989"),(19, "0.999999999999999999999999999999999999999"),
      (20, "1.000000000000000000000000000000000000009"),(21, "1.000000000000000000000000000000000000019"),(22, "1.000000000000000000000000000000000000099"),(23, "1.099999999999999999999999999999999999999"),(24, "-0.000000000000000000000000000000000000003"),(25, "-0.000000000000000000000000000000000000013"),(26, "-0.000000000000000000000000000000000000093"),(27, "-0.099999999999999999999999999999999999993"),(28, "-0.900000000000000000000000000000000000003"),(29, "-0.900000000000000000000000000000000000013"),(30, "-0.999999999999999999999999999999999999983"),(31, "-0.999999999999999999999999999999999999993"),(32, "-1.000000000000000000000000000000000000003"),(33, "-1.000000000000000000000000000000000000013"),(34, "-1.000000000000000000000000000000000000093"),(35, "-1.099999999999999999999999999999999999993"),(36, "-0.000000000000000000000000000000000000009"),(37, "-0.000000000000000000000000000000000000019"),(38, "-0.000000000000000000000000000000000000099"),(39, "-0.099999999999999999999999999999999999999"),
      (40, "-0.900000000000000000000000000000000000009"),(41, "-0.900000000000000000000000000000000000019"),(42, "-0.999999999999999999999999999999999999989"),(43, "-0.999999999999999999999999999999999999999"),(44, "-1.000000000000000000000000000000000000009"),(45, "-1.000000000000000000000000000000000000019"),(46, "-1.000000000000000000000000000000000000099"),(47, "-1.099999999999999999999999999999999999999");
    """

    sql "set enable_strict_cast=true;"
    qt_sql_1_strict 'select f1, cast(f2 as decimalv3(76, 38)) from test_cast_to_decimal_76_38_from_double_1_not_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_1_non_strict 'select f1, cast(f2 as decimalv3(76, 38)) from test_cast_to_decimal_76_38_from_double_1_not_nullable order by 1;'

    sql "drop table if exists test_cast_to_decimal_76_75_from_double_2_nullable;"
    sql "create table test_cast_to_decimal_76_75_from_double_2_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_75_from_double_2_nullable values (0, "0.0000000000000000000000000000000000000000000000000000000000000000000000000003"),(1, "0.0000000000000000000000000000000000000000000000000000000000000000000000000013"),(2, "0.0000000000000000000000000000000000000000000000000000000000000000000000000093"),(3, "0.0000000000000000000000000000000000000000000000000000000000000000000000000009"),(4, "0.0000000000000000000000000000000000000000000000000000000000000000000000000019"),(5, "0.0000000000000000000000000000000000000000000000000000000000000000000000000099"),(6, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000003"),(7, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000013"),(8, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000093"),(9, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000009"),(10, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000019"),(11, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000099")
      ,(12, null);
    """

    sql "set enable_strict_cast=true;"
    qt_sql_2_strict 'select f1, cast(f2 as decimalv3(76, 75)) from test_cast_to_decimal_76_75_from_double_2_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_2_non_strict 'select f1, cast(f2 as decimalv3(76, 75)) from test_cast_to_decimal_76_75_from_double_2_nullable order by 1;'

    sql "drop table if exists test_cast_to_decimal_76_75_from_double_2_not_nullable;"
    sql "create table test_cast_to_decimal_76_75_from_double_2_not_nullable(f1 int, f2 double) properties('replication_num'='1');"
    sql """insert into test_cast_to_decimal_76_75_from_double_2_not_nullable values (0, "0.0000000000000000000000000000000000000000000000000000000000000000000000000003"),(1, "0.0000000000000000000000000000000000000000000000000000000000000000000000000013"),(2, "0.0000000000000000000000000000000000000000000000000000000000000000000000000093"),(3, "0.0000000000000000000000000000000000000000000000000000000000000000000000000009"),(4, "0.0000000000000000000000000000000000000000000000000000000000000000000000000019"),(5, "0.0000000000000000000000000000000000000000000000000000000000000000000000000099"),(6, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000003"),(7, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000013"),(8, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000093"),(9, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000009"),(10, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000019"),(11, "-0.0000000000000000000000000000000000000000000000000000000000000000000000000099");
    """

    sql "set enable_strict_cast=true;"
    qt_sql_2_strict 'select f1, cast(f2 as decimalv3(76, 75)) from test_cast_to_decimal_76_75_from_double_2_not_nullable order by 1;'

    sql "set enable_strict_cast=false;"
    qt_sql_2_non_strict 'select f1, cast(f2 as decimalv3(76, 75)) from test_cast_to_decimal_76_75_from_double_2_not_nullable order by 1;'

}